﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;

namespace PMS.ChucNang
{
    public partial class PhieuGiaoLookup : FormPopupBase
    {
        private readonly PhieuGiaoHangBO service = new PhieuGiaoHangBO();
        public PhieuGiaoHang PhieuGiaoHangSelected;

        public PhieuGiaoLookup()
        {
            InitializeComponent();
            InitStyle();
            InitData();
        }
      
        void InitStyle()
        {
            grvPhieuGiaoHangList.OptionsBehavior.Editable = false;
            grvPhieuGiaoHangList.OptionsView.ShowGroupPanel = false;
            grvPhieuGiaoHangList.OptionsView.ShowIndicator = true;

            grvPhieuGiaoHangList.OptionsView.ShowAutoFilterRow = true;
            grvPhieuGiaoHangList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvPhieuGiaoHangList.OptionsView.ShowGroupedColumns = false;
            grvPhieuGiaoHangList.OptionsMenu.EnableColumnMenu = false;
            grvPhieuGiaoHangList.OptionsCustomization.AllowColumnMoving = false;
            grvPhieuGiaoHangList.OptionsCustomization.AllowGroup = false;
            grvPhieuGiaoHangList.OptionsFilter.AllowFilterEditor = false;

            this.grvPhieuGiaoHangList.OptionsSelection.MultiSelect = false;
            this.grvPhieuGiaoHangList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colSoPG.FieldName = PhieuGiaoHangColumns.SoPG.ToString();
            this.colMaKH.FieldName = PhieuGiaoHangColumns.MaKH.ToString();
            this.colSoHD.FieldName = PhieuGiaoHangColumns.SoHD.ToString();
            this.colSoPX.FieldName = PhieuGiaoHangColumns.SoPX.ToString();
            this.colNgayGiao.FieldName = PhieuGiaoHangColumns.NgayGiao.ToString();
            this.colNguoiGiao.FieldName = PhieuGiaoHangColumns.NguoiGiao.ToString();
            this.colNguoiNhan.FieldName = PhieuGiaoHangColumns.NguoiNhan.ToString();
            this.colPhuongThucGiao.FieldName = PhieuGiaoHangColumns.PhuongThucGiao.ToString();
            //this.colThanhTien.FieldName = PhieuGiaoHangColumns.GhiChu.ToString();
            this.colPhuongThucTT.FieldName = PhieuGiaoHangColumns.PhuongThucTT.ToString();


        }

        void InitData()
        {
            try
            {
                this.grdPhieuGiaoHangList.DataSource = service.GetAllPhieuGiaoHang(); 
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.PhieuGiaoHangSelected = (PhieuGiaoHang)grvPhieuGiaoHangList.GetFocusedRow();
                }
                else
                {
                    this.PhieuGiaoHangSelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvPhieuGiaoHangList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvPhieuGiaoHangList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdPhieuGiaoHangList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (PhieuGiaoHangSelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvPhieuGiaoHangList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvPhieuGiaoHangList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            PhieuGiaoHangSelected = this.grvPhieuGiaoHangList.GetFocusedRow() as PhieuGiaoHang;
        }

        private void grvPhieuGiaoHangList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            this.grdPhieuGiaoHangList.DataSource = service.GetPhieuGiaoHangByDate((DateTime?)dateTuNgay.EditValue, (DateTime?)dateDenNgay.EditValue);
        }
    }
}
